package com.dhecp.project.hr.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.hr.domain.TrainingPlanEntryFormDetail;
import com.dhecp.project.hr.mapper.TrainingPlanEntryFormDetailMapper;
import com.dhecp.project.hr.service.TrainingPlanEntryFormDetailService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 培训报名表 服务层
 * @author DH-lwl
 * @version 2020年4月26日
 */
@Service
public class TrainingPlanEntryFormDetailServiceImpl implements TrainingPlanEntryFormDetailService {

	@Autowired
	private TrainingPlanEntryFormDetailMapper businessMapper;
	
	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;
	
	/**
	 * 查询指定Guid的记录
	 * @param guid 行唯一值
	 * @return TrainingPlanEntryFormDetail 实体
	 * @author DH-Xxj
	 */
	@Override
	public TrainingPlanEntryFormDetail queryByGuid(Map<String, Object> params) {
		return businessMapper.selectByGuid(params);
	}
	
	/**
	 * 查询
	 * @param params
	 * @return List - TrainingPlanEntryFormDetail
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
    public List<TrainingPlanEntryFormDetail> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        PageUtils.startPageStronger();
        return businessMapper.select(params);
    }

	/**
	 * 修改报名状态
	 * @param infoItems 必要参数：对应实体类的json格式字符串（需包含：guid,bBmZz,dBmQx，其中bBmZz有效值：已报名、忽略、待报名）
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
	@Override
    public AjaxResult updateBBmZz(Map<String, Object> params) {
        List<TrainingPlanEntryFormDetail> businessDataList = JSON.parseArray(params.get("infoItems").toString(), TrainingPlanEntryFormDetail.class);
        if (businessDataList == null || businessDataList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
		Date nowDate = new Date();
        for (TrainingPlanEntryFormDetail businessData : businessDataList) {
        	if (businessData.getbBmZz() != null){
        		if (!"已报名".equals(businessData.getbBmZz().toString()) && !"忽略".equals(businessData.getbBmZz().toString())){
        			businessData.setbBmZz("待报名");
        		}else if ("已报名".equals(businessData.getbBmZz().toString())){
        			if (businessData.getdBmQx() == null){
        				businessData.setdBmQx(nowDate);
        			}
        			if (businessData.getdBmQx().before(nowDate)){
        				return AjaxResult.error("报名已结束。");
        			}
        		}
        	}
        	businessData.setdBmSj(nowDate);
        	businessMapper.updateBBmZz(businessData);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

}
